import { Component, toNative } from 'vue-facing-decorator'
import TableRender, { type TableRenderColumnTypes } from '@/render/TableRender'
import type { FilterDto } from '@/render/FilterRender'
import type { AnyObject } from '@/types'
import PreviewVideoDialog from '@/components/preview-media/PreviewVideoDialog'
import { DailyProductTypeCnEnum, getKeyToEnum, InsuranceDailyStatusCnEnum, OccupationAuditStatusCnEnum } from '@/const/enum'
import { aduitDailyAndGroup, getWesureAuditList } from '@/api/insurance/daily'
import type { WesureAuditDto } from '@/api/insurance/daily/types'
import PreviewVideoOrImage from '@/components/preview-media/PreviewVideoOrImage'
import DialogUpdateIndustry from '../daily/components/DialogUpdateIndustry'
import DialogReject from '../daily/components/DialogReject'
import { confirm } from '@/utils/util'
import { message } from 'ant-design-vue'

@Component
export class DailyView extends TableRender {
    curData = {} as WesureAuditDto

    showDialog = false
    showReject = false

    filterConfig() {
        return {
            companyId: {
                name: '客户名称',
                element: 'select',
                optionsType: 'company'
            },
            serveCompanyName: {
                name: '派遣单位',
                element: 'input',
            },
            auditStatus: {
                name: '审核状态',
                element: 'select',
                options: getKeyToEnum(InsuranceDailyStatusCnEnum, true, ['待审核', '已通过', '已驳回'])
            }
        } as FilterDto
    }

    tableColumnConfigOld = () =>
        [
            {
                field: 'operate',
                title: '操作',
                width: 60,
                fixed: 'left',
                params: {
                    buttons: [
                        {
                            text: '修改行业',
                            type: 'primary',
                            hidden: (row)=> row.auditStatus !== InsuranceDailyStatusCnEnum.已通过 && row.auditStatus !== InsuranceDailyStatusCnEnum.待审核,
                            event: (row) => {
                                this.handleEditIndustry(row)
                            }
                        },
                        {
                            text: '通过',
                            type: 'primary',
                            hidden: (row)=> row.auditStatus !== InsuranceDailyStatusCnEnum.待审核,
                            event: (row) => {
                                this.handleAudit(row)
                            }
                        },
                        {
                            text: '驳回',
                            hidden: (row)=> row.auditStatus !== InsuranceDailyStatusCnEnum.待审核,
                            event: (row) => {
                                this.handleReject(row)
                            }
                        }
                    ]
                },
                slots: {
                    default: 'tooltipButtons'
                }
            },
            {
                field: 'companyName',
                title: '客户名',
                width: 200,
                // params: {
                //     path: (row) => `/insurance-center/dispatching-company/${row.id}`,
                // },
                // slots: {
                //     default: 'toNext'
                // }
            },
            {
                field: 'serveCompanyName',
                title: '派遣单位',
                width: 200,
            },
            {
                field: 'auditStatus',
                title: '审批状态',
                params: {
                    tags: {
                        0: 'info',
                        1: 'success',
                        2: 'error',
                        3: 'processing'
                    }
                },
                formatter: ({ row }) => {
                    return InsuranceDailyStatusCnEnum[row.auditStatus]
                },
                slots: {
                    default: 'colorTag'
                }
            },
            {
                field: 'contacts',
                title: '负责人',
            },
            {
                field: 'phone',
                title: '手机号',
                width: 120
            },
            {
                field: 'file',
                title: '审核视频',
                slots: {
                    default: ({row}) => {
                        return row.params?.file ? <PreviewVideoOrImage src={row.params.file} /> : '--'
                    }
                }
            },
            {
                field: 'applySource',
                title: '申请来源',
                formatter: ({ row }) => {
                    return row.dailyInfo.applySource === 0 ? '日日保' : row.dailyInfo.applySource === 1 ? '团意险' : '--'
                }
            },
            {
                field: 'industryName',
                title: '行业名称',
                formatter: ({ row }) => {
                    return row.dailyInfo?.industryName || '--'
                }
            },
        ] as TableRenderColumnTypes<WesureAuditDto>

    async handleEditIndustry(row: WesureAuditDto) {
        this.showDialog = true
        this.curData = row
    }

    async handleAudit(row: WesureAuditDto) {
        await confirm('确定通过该条审核么？', '提示')
        await aduitDailyAndGroup({
            ...row,
            insureCompanyType: 'insure002_gsc',
            auditStatus: 1
        })
        message.success('审核成功')
        this.reloadPage()
    }

    handleReject(row: WesureAuditDto) {
        this.showReject = true
        this.curData = row
    }

    onLoadData(page: number, size: number, filters: AnyObject) {
        return getWesureAuditList({
            pageSize: size,
            pageNum: page,
            ...filters,
            insureCompanyType: 'insure002_gsc',
            productType: DailyProductTypeCnEnum.团意险
        })
    }

    render() {
        return <>
            {super.render()}
            <DialogUpdateIndustry v-model:show={this.showDialog} serveCompanyId={this.curData.id} productType={DailyProductTypeCnEnum.团意险} onRefresh={this.reloadPage}/>
            <DialogReject v-model:show={this.showReject} detailData={this.curData} onRefresh={this.reloadPage} />
        </>
    }
}

export default toNative(DailyView)
